package com.khh.base.util;

import java.io.ByteArrayOutputStream;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.security.MessageDigest;
import java.text.DateFormat;
import java.util.Calendar;
import java.util.Collection;
import java.util.Date;
import java.util.Iterator;
import java.util.Locale;
import java.util.UUID;

import com.khh.base.assertion.AssertValue;

public class CommonUtils {

	public static String encodingConvert(String strvalue, String oldCodeType,
			String newCodeType) {
		try {
			if (strvalue == null) {
				return null;
			} else {
				strvalue = new String(strvalue.getBytes(oldCodeType),
						newCodeType);
				return strvalue;
			}
		} catch (Exception e) {
			return null;
		}
	}

	public static String toUtf8ToGBK(String strvalue) {
		try {
			if (strvalue == null) {
				return null;
			} else {
				strvalue = new String(strvalue.getBytes("utf8"), "GBK");
				return strvalue;
			}
		} catch (Exception e) {
			return null;
		}
	}

	public final static String MD5(String s) {
		char hexDigits[] = { '0', '1', '2', '3', '4', '5', '6', '7', '8', '9',
				'a', 'b', 'c', 'd', 'e', 'f' };
		try {
			byte[] strTemp = s.getBytes();
			MessageDigest mdTemp = MessageDigest.getInstance("MD5");
			mdTemp.update(strTemp);
			byte[] md = mdTemp.digest();
			int j = md.length;
			char str[] = new char[j * 2];
			int k = 0;
			for (int i = 0; i < j; i++) {
				byte byte0 = md[i];
				str[k++] = hexDigits[byte0 >>> 4 & 0xf];
				str[k++] = hexDigits[byte0 & 0xf];
			}
			return new String(str);
		} catch (Exception e) {
			return null;
		}
	}

	public static boolean checkWsPassword(String password) {
		if (password == null || password.trim().length() <= 0) {
			return false;
		}
		if (password.equals(MD5("zdsoft").trim())) {
			return true;
		} else {
			return false;
		}
	}

	public static String getWSPassword() {
		return MD5("zdsoft");
	}

	public static String getLocalDateString() {
		return getLocalDateString(new Date());
	}

	public static String getLocalDateString(Date date) {
		if (date == null) {
			return "";
		}
		Locale local = new Locale("zh", "CN");
		DateFormat format = DateFormat.getDateTimeInstance(DateFormat.LONG,
				DateFormat.LONG, local);
		return format.format(date).toString();
	}

	public static String getXmlHeader(String version, String encoding) {
		if (encoding == null) {
			encoding = "UTF-8";
		}

		if (encoding.equals("utf8")) {
			encoding = "UTF-8";
		}

		if (version == null || version.trim().length() <= 0) {
			version = "1.0";
		}

		return "<?xml version=\"" + version + "\" encoding=\"" + encoding
				+ "\"?>";
	}

	public static byte[] readFile(String path) throws FileNotFoundException,
			IOException {

		if (path == null || path.trim().length() <= 0) {
			return null;
		}
		FileInputStream fis = null;
		try {
			byte[] classbytes = null;

			fis = new FileInputStream(path);
			ByteArrayOutputStream baos = new ByteArrayOutputStream();
			int ch = 0;
			while ((ch = fis.read()) != -1) {
				baos.write(ch);
			}
			classbytes = baos.toByteArray();

			return classbytes;
		} finally {
			fis.close();
		}

	}

	public static String getYMDString(String pre) {
		String temp = getYMDString();

		if (AssertValue.isNotNullAndNotEmpty(pre)) {
			temp = temp.substring(pre.length(), temp.length());
			return pre + temp;
		} else {
			return temp;
		}
	}

	public static String getYMDString() {
		Calendar calendar = Calendar.getInstance();

		String y = calendar.get(Calendar.YEAR) + "";

		int mtemp = calendar.get(Calendar.MONDAY);
		mtemp = mtemp + 1;
		String m = mtemp + "";
		String d = calendar.get(Calendar.DAY_OF_MONTH) + "";

		m = "00000" + m;
		m = m.substring(m.length() - 2, m.length());

		d = "00000" + d;
		d = d.substring(d.length() - 2, d.length());

		return y + m + d;
	}

	public static int getMonthDayNum(int pre) {
		Calendar calendar = Calendar.getInstance();

		int m = calendar.get(Calendar.MONTH);

		calendar.add(Calendar.MONTH, pre);

		return calendar.getActualMaximum(Calendar.DAY_OF_MONTH);

	}

	public static int getMonthDayNum() {

		Calendar calendar = Calendar.getInstance();

		return calendar.getActualMaximum(Calendar.DAY_OF_MONTH);

	}

	// 将127.0.0.1 形式的ip地址转换成10进制整数，这里没有进行任何错误处理
	public static long iptolong(String strip) {
		int j = 0;
		int i = 0;

		long[] ip = new long[4];
		int position1 = strip.indexOf(".");
		int position2 = strip.indexOf(".", position1 + 1);
		int position3 = strip.indexOf(".", position2 + 1);
		ip[0] = Long.parseLong(strip.substring(0, position1));
		ip[1] = Long.parseLong(strip.substring(position1 + 1, position2));
		ip[2] = Long.parseLong(strip.substring(position2 + 1, position3));
		ip[3] = Long.parseLong(strip.substring(position3 + 1));
		return (ip[0] << 24) + (ip[1] << 16) + (ip[2] << 8) + ip[3]; // ip1*256*256*256+ip2*256*256+ip3*256+ip4
	}

	// 将10进制整数形式转换成127.0.0.1形式的ip地址，在命令提示符下输入ping 3396362403l
	public static String longtoip(long longip) {
		StringBuffer sb = new StringBuffer("");
		sb.append(String.valueOf(longip >>> 24));// 直接右移24位
		sb.append(".");
		sb.append(String.valueOf((longip & 0x00ffffff) >>> 16)); // 将高8位置0，然后右移16位
		sb.append(".");
		sb.append(String.valueOf((longip & 0x0000ffff) >>> 8));
		sb.append(".");
		sb.append(String.valueOf(longip & 0x000000ff));
		sb.append(".");
		return sb.toString();
	}

	public static int getDayOfWeek(Date date) {
		Calendar c = Calendar.getInstance();

		c.setTime(date);

		int dayOfWeek = c.get(Calendar.DAY_OF_WEEK);

		int weekName = 0;

		switch (dayOfWeek) {
		case 1:
			weekName = 7;
			break;
		case 2:
			weekName = 1;
			break;
		case 3:
			weekName = 2;
			break;
		case 4:
			weekName = 3;
			break;
		case 5:
			weekName = 4;
			break;
		case 6:
			weekName = 5;
			break;
		case 7:
			weekName = 6;
			break;
		}

		return weekName;
	}

	public static String getDayOfWeekCN(Date date) {
		Calendar c = Calendar.getInstance();

		c.setTime(date);

		int dayOfWeek = c.get(Calendar.DAY_OF_WEEK);

		String weekName = "";

		switch (dayOfWeek) {
		case 1:
			weekName = "星期日";
			break;
		case 2:
			weekName = "星期一";
			break;
		case 3:
			weekName = "星期二";
			break;
		case 4:
			weekName = "星期三";
			break;
		case 5:
			weekName = "星期四";
			break;
		case 6:
			weekName = "星期五";
			break;
		case 7:
			weekName = "星期六";
			break;
		}

		return weekName;
	}
	
	
	/**
	 * 生成一个
	 * @return
	 */
	public static String generateSN() {
		UUID uuid = UUID.randomUUID();
		return uuid.toString();
	}
	
	/**
	 * 生成一组
	 * @param uuidsize 需要生成的数量
	 * @return
	 */
	public static String[] generateSNs(int uuidsize) {
		String[] result = new String[uuidsize];
		for(int i = 0 ; i < uuidsize ; i++){
			UUID uuid = UUID.randomUUID();
			result[i] = uuid.toString();
		}
		return result;
	}
	
	/**
	 * 根据数组返回字符串
	 */

	public static String toString(Collection<String> c){
		if(null == c){
			return "";
		}
		Iterator it =c.iterator();
		StringBuffer sb = new StringBuffer();
		while(it.hasNext()){
			String nString = String.valueOf(it.next());
			sb.append(nString);
		}
		return sb.toString();
	}
}
